home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK2.toast / Development Kits (Disc 2) / TEC 1.2.1 / Sample Code / DropEncoder / Source / CustomApp.c next >
Encoding:
C/C++ Source or Header  |  1997-11-18  |  6.4 KB  |  311 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #    CustomApp.c
  4. #   
  5. #   CustomApp.c includes many of the routines required to customize the shell app, such
  6. #   as initialization and termination routines.  Ideally, shell.c should not need to be
  7. #   modified.
  8. #
  9. #    Author: Timothy Carroll
  10. #    Apple Developer Technical Support
  11. #    timc@apple.com
  12. #
  13. #    Revision: Jason Yeo
  14. #
  15. #    Modification History: 
  16. #
  17. #    2/9/97        TMC     Initial Release
  18. #
  19. #    9/12/97        JY         Updated for:
  20. #                        TEC 1.2.1 
  21. #                        Universal Interfaces 3.0
  22. #                        CodeWarrior 11 projects
  23. #
  24. #    Copyright © 1997 Apple Computer, Inc., All Rights Reserved
  25. #
  26. #
  27. #    You may incorporate this sample code into your applications without
  28. #    restriction, though the sample code has been provided "AS IS" and the
  29. #    responsibility for its operation is 100% yours.  However, what you are
  30. #    not permitted to do is to redistribute the source as "DSC Sample Code"
  31. #    after having made changes. If you're going to re-distribute the source,
  32. #    we require that you make it clear in the source that the code was
  33. #    descended from Apple Sample Code, but that you've made changes.
  34. #
  35. *************************************************************************************/
  36.  
  37. #include "CustomApp.h"
  38. #include "Encoder.h"
  39. #include "Sound.h"
  40.  
  41. static OSStatus HandleMenus (SInt32 menuResult);
  42.  
  43.  
  44. OSStatus InitApp (void)
  45. {
  46.     OSStatus    theErr         = noErr;
  47.     Handle        menuBarH     = NULL;
  48.     MenuRef        appleMenuH     = NULL;
  49.     
  50.     // Load and install the menu bar.
  51.     menuBarH = GetNewMBar(rMenuBar);
  52.     FAIL_NIL (menuBarH, "\pError: Unable to load the Menubar.")
  53.  
  54.     SetMenuBar(menuBarH);
  55.     DisposeHandle(menuBarH);
  56.     
  57.     // Add all of the standard items to the Apple Menu.
  58.     appleMenuH = GetMenuHandle(mAppleMenu);
  59.     FAIL_NIL (appleMenuH, "\pError: Couldn't find Apple Menu")
  60.     AppendResMenu(appleMenuH, 'DRVR');
  61.     
  62.     DrawMenuBar();
  63.  
  64.     // We're done, go cleanup
  65.      goto cleanup;
  66.      
  67. error:
  68.     if (theErr == noErr)
  69.         theErr = paramErr;
  70.         
  71. cleanup:
  72.     return theErr;
  73. }
  74.  
  75.  
  76. OSStatus TerminateApp (void)
  77. {
  78.  
  79.     return noErr;
  80. }
  81.  
  82.  
  83. Boolean AppWantsTime (void)
  84. {
  85.     return false;
  86. }
  87.  
  88.  
  89. OSStatus TimeSlice (void)
  90. {
  91.     return noErr;
  92. }
  93.  
  94. OSStatus HandleEvent (EventRecord *theEvent)
  95. {
  96.     OSStatus    theErr = noErr;
  97.     WindowRef    whichWindow = NULL;
  98.     SInt16         part;
  99.  
  100.     switch (theEvent->what)
  101.     {
  102.         case nullEvent:
  103.             break;
  104.             
  105.         case mouseDown:
  106.             part = FindWindow(theEvent->where,&whichWindow);
  107.             switch(part)
  108.             {
  109.                 case inMenuBar:
  110.                     theErr = HandleMenus(MenuSelect(theEvent->where));
  111.                     break;
  112.         
  113.                 case inSysWindow:
  114.                     SystemClick(theEvent, (GrafPtr) whichWindow);
  115.                     break;
  116.                     
  117.                 default:
  118.                     break;
  119.             }
  120.             break;
  121.             
  122.         case osEvt:
  123.             switch ((theEvent->message & osEvtMessageMask) >> 24)
  124.             {
  125.                 case mouseMovedMessage:
  126.                     break;
  127.                     
  128.                 case suspendResumeMessage:                    
  129.                     gAppForeGround = !gAppForeGround;
  130.                     break;
  131.             }
  132.             break;
  133.         
  134.         case kHighLevelEvent:
  135.             theErr = AEProcessAppleEvent(theEvent);
  136.             break;
  137.         
  138.         case keyDown:
  139.             if (theEvent->modifiers & cmdKey)
  140.             {
  141.                 theErr = HandleMenus (MenuKey((short) theEvent->message & charCodeMask));
  142.             }
  143.             else
  144.             {
  145.             
  146.             }
  147.             break;
  148.  
  149.         default:
  150.             break;
  151.     };
  152.  
  153.  
  154.     InitCursor ();  // Return the cursor to a standard arrow.
  155.     return theErr;
  156. }
  157.  
  158. pascal OSErr HandleOpenAppEvent (AppleEvent *event, AppleEvent *reply,long refCon)
  159. {
  160.     OSErr theErr = noErr;
  161.     theErr = CheckAppleEventForMissingParams (event);
  162.     if (theErr != noErr)
  163.         return theErr;
  164.     
  165.     // Do we want to do anything special here?  I doubt it!
  166.     return noErr;
  167.  
  168. #pragma unused reply
  169. #pragma unused refcon
  170. }
  171.  
  172.  
  173. pascal OSErr HandleOpenDocEvent (AppleEvent *event, AppleEvent *reply,long refCon)
  174. {
  175.     OSErr         theErr = noErr;
  176.     AEDescList    docsList;
  177.     SInt32        loop, docCount;
  178.     DescType    type;
  179.     Size        docSize;
  180.     FSSpec        file;
  181.     AEKeyword    keyword;
  182.     
  183.     docsList.dataHandle = NULL;
  184.     
  185.     theErr = AEGetParamDesc(event,keyDirectObject,typeAEList,&docsList);
  186.     FAIL_OSERR (theErr, "\pError: Failed to retrieve document list in ODOC")
  187.     
  188.     theErr = CheckAppleEventForMissingParams (event);
  189.     FAIL_OSERR (theErr, "\pError: Missing parameters in ODOC")
  190.     
  191.     // Find out how many items were passed into us.
  192.     theErr = AECountItems (&docsList, &docCount);
  193.     FAIL_OSERR (theErr, "\pError: Failed to get a document count in ODOC")
  194.     
  195.     for (loop = 1; loop <= docCount; loop++)
  196.     {
  197.         theErr = AEGetNthPtr(&docsList,loop,typeFSS,&keyword,&type,
  198.                                 (Ptr) &file,sizeof(file),&docSize);
  199.         FAIL_OSERR (theErr, "\pError: Failed to retrieve the FSSpec in ODOC")
  200.         
  201.         EncodeFile (&file);
  202.     }
  203.     
  204.     goto cleanup;
  205.     
  206. error:
  207.     if (theErr == noErr)
  208.         theErr = paramErr;
  209. cleanup:
  210.     if (docsList.dataHandle != NULL)
  211.         (void) AEDisposeDesc(&docsList);
  212.     return theErr;
  213.     
  214.     
  215.  
  216. #pragma unused reply
  217. #pragma unused refcon
  218. }
  219.  
  220. pascal OSErr HandlePrintDocEvent (AppleEvent *event, AppleEvent *reply,long refCon)
  221. {
  222.     // We don't do printing
  223.     return errAEEventNotHandled;
  224.  
  225. #pragma unused event
  226. #pragma unused reply
  227. #pragma unused refcon
  228. }
  229.  
  230. pascal OSErr HandleQuitAppEvent (AppleEvent *event, AppleEvent *reply,long refCon)
  231. {
  232.     OSErr theErr = noErr;
  233.     theErr = CheckAppleEventForMissingParams (event);
  234.     if (theErr != noErr)
  235.         return theErr;
  236.  
  237.     gQuittingApp = true;
  238.     return theErr;
  239.  
  240. #pragma unused reply
  241. #pragma unused refcon
  242. }
  243.  
  244.  
  245. OSStatus HandleMenus (SInt32 menuResult)
  246. {
  247.     OSStatus theErr = noErr;
  248.     SInt16 theMenu, theItem;
  249.  
  250.     theMenu = (menuResult >> 16) & 0x0000FFFF;
  251.     theItem = (menuResult) & 0x0000FFFF;
  252.                         
  253.     switch (theMenu)
  254.     {
  255.         case mAppleMenu:
  256.             if (theItem == iAboutApp)
  257.                 SysBeep(2);
  258.             else
  259.             {
  260.                 // Find out the name of the Apple menu item and open the "desk accessory"
  261.                 Str255 name;
  262.                 MenuHandle appleMenuH = NULL;
  263.                 
  264.                 appleMenuH = GetMenuHandle (mAppleMenu);
  265.                 FAIL_NIL (appleMenuH, "\pError: Unable to retrieve the Apple menu")
  266.                 
  267.                 GetMenuItemText(appleMenuH,theItem,name);
  268.                 (void) OpenDeskAcc(name);
  269.             }
  270.             break;
  271.         case mFileMenu:
  272.             switch (theItem)
  273.             {
  274.                 case iConvert:
  275.                     // Open a standard files dialog and choose a text file to convert.
  276.                     // TO DO: This should generate an Apple Event instead of directly calling.
  277.                     {
  278.                         StandardFileReply        inputFile;
  279.                         SFTypeList                typeList;
  280.                         
  281.                         typeList[0] = 'TEXT';
  282.                         
  283.                         StandardGetFile (NULL, 1, typeList, &inputFile);
  284.                         
  285.                         if (inputFile.sfGood)
  286.                             EncodeFile (&inputFile.sfFile);
  287.                     }
  288.                     break;                    
  289.                     
  290.                 case iQuit:
  291.                     theErr = DispatchQuitEvent();
  292.                     break;
  293.             };
  294.             break;
  295.             
  296.         case mEditMenu:
  297.             if (theItem == iPreferences)
  298.             theErr = EditPreferences (&gPreferences);
  299.             break;
  300.     }
  301.  
  302.     goto cleanup;
  303.         
  304. error:
  305.     if (theErr == noErr)
  306.         theErr = paramErr;
  307.         
  308. cleanup:
  309.     HiliteMenu(0);
  310.     return theErr;
  311. }